package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaDevice;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceType;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbaDeviceTypeDao {

    /**
     * 查询所有
     *
     * @return
     */
    @Results(id = "pcbaDeviceTypeResultMap", value = {
            @Result(column = "pcba_device_type_id", property = "pcbaDeviceTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_type_name_cn", property = "pcbaDeviceTypeNameCn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_device_type_name_en", property = "pcbaDeviceTypeNameEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_tech_own", property = "isTechOwn", jdbcType = JdbcType.INTEGER),
            @Result(column = "device_picture", property = "devicePicture", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pic_length", property = "picLength", jdbcType = JdbcType.INTEGER),
            @Result(column = "pic_width", property = "picWidth", jdbcType = JdbcType.INTEGER),
    })
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_DEVICE_TYPE + " where 1 = 1 " +
            "<if test='searchValue != null and searchValue != \"\" '>" +
            " and (pcba_device_type_name_cn like CONCAT('%',#{searchValue},'%') or pcba_device_type_name_en like CONCAT('%',#{searchValue},'%'))" +
            "</if>" +
            "<if test='isTechOwn != null and isTechOwn != \"\" '>" +
            " and is_tech_own = #{isTechOwn}" +
            "</if>" +
            " order by is_tech_own desc, pcba_device_type_id" +
            "</script>")
    List<PcbaDeviceType> getAllPcbaDeviceType(@Param("searchValue") String searchValue, @Param("isTechOwn") Integer isTechOwn);

    /**
     * 判断有没有重复的
     */
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_DEVICE_TYPE + " where pcba_device_type_name_cn = #{pcbaDeviceTypeNameCn} " +
            "<if test='pcbaDeviceTypeId != null and pcbaDeviceTypeId != \"\" '>" +
            " and pcba_device_type_id != #{pcbaDeviceTypeId}" +
            "</if>" +
            "</script>")
    @ResultMap(value = "pcbaDeviceTypeResultMap")
    List<PcbaDeviceType> judgeUniquePcbaDeviceType(@Param("pcbaDeviceTypeNameCn") String pcbaDeviceTypeNameCn, @Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId);

    /**
     * 根据设备中文名查询
     *
     * @param pcbaDeviceTypeNameCn
     * @return
     */
    @ResultMap(value = "pcbaDeviceTypeResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_TYPE + " where pcba_device_type_name_cn = #{pcbaDeviceTypeNameCn}")
    PcbaDeviceType getPcbaDeviceTypeByNameCn(@Param("pcbaDeviceTypeNameCn") String pcbaDeviceTypeNameCn);

    /**
     * 根据id查询
     *
     * @param pcbaDeviceTypeId
     * @return
     */
    @ResultMap(value = "pcbaDeviceTypeResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_TYPE + " where pcba_device_type_id = #{pcbaDeviceTypeId}")
    PcbaDeviceType getById(@Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId);

    /**
     * 删除
     *
     * @param pcbaDeviceTypeId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_DEVICE_TYPE + " where pcba_device_type_id = #{pcbaDeviceTypeId}")
    Integer deletePcbaDeviceType(@Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId);

    /**
     * 新增
     *
     * @param pcbaDeviceType
     * @return
     */
    @Insert("insert into " + DB1TableName.PCBA_DEVICE_TYPE + "(pcba_device_type_name_cn,pcba_device_type_name_en,create_time,status,sort,is_tech_own,device_picture,pic_length,pic_width) values " +
            "(#{pcbaDeviceType.pcbaDeviceTypeNameCn},#{pcbaDeviceType.pcbaDeviceTypeNameEn},#{pcbaDeviceType.createTime},#{pcbaDeviceType.status},#{pcbaDeviceType.sort},#{pcbaDeviceType.isTechOwn},#{pcbaDeviceType.devicePicture},#{pcbaDeviceType.picLength},#{pcbaDeviceType.picWidth})")
    // 显式设置主键
    @Options(useGeneratedKeys = true, keyProperty = "pcbaDeviceType.pcbaDeviceTypeId")
    Integer addPcbaDeviceType(@Param("pcbaDeviceType") PcbaDeviceType pcbaDeviceType);

    /**
     * 更新
     */
    @Update("<script>" +
            "update " + DB1TableName.PCBA_DEVICE_TYPE +
            "<set>" +
            "<if test='pcbaDeviceType.pcbaDeviceTypeNameCn != null and pcbaDeviceType.pcbaDeviceTypeNameCn != \"\" '>" +
            " pcba_device_type_name_cn = #{pcbaDeviceType.pcbaDeviceTypeNameCn}," +
            "</if>" +
            "<if test='pcbaDeviceType.pcbaDeviceTypeNameEn != null and pcbaDeviceType.pcbaDeviceTypeNameEn != \"\" '>" +
            " pcba_device_type_name_en = #{pcbaDeviceType.pcbaDeviceTypeNameEn}," +
            "</if>" +
            "<if test='pcbaDeviceType.updateTime != null '>" +
            " update_time = #{pcbaDeviceType.updateTime}," +
            "</if>" +
            "<if test='pcbaDeviceType.status != null and pcbaDeviceType.status != \"\" '>" +
            " status = #{pcbaDeviceType.status}," +
            "</if>" +
            "<if test='pcbaDeviceType.sort != null and pcbaDeviceType.sort != \"\" '>" +
            " sort = #{pcbaDeviceType.sort}," +
            "</if>" +
            "<if test='pcbaDeviceType.isTechOwn != null and pcbaDeviceType.isTechOwn != \"\" '>" +
            " is_tech_own = #{pcbaDeviceType.isTechOwn}," +
            "</if>" +
            "<if test='pcbaDeviceType.devicePicture != null and pcbaDeviceType.devicePicture != \"\" '>" +
            " device_picture = #{pcbaDeviceType.devicePicture}," +
            "</if>" +
            "<if test='pcbaDeviceType.picLength != null and pcbaDeviceType.picLength != \"\" '>" +
            " pic_length = #{pcbaDeviceType.picLength}," +
            "</if>" +
            "<if test='pcbaDeviceType.picWidth != null and pcbaDeviceType.picWidth != \"\" '>" +
            " pic_width = #{pcbaDeviceType.picWidth}," +
            "</if>" +
            "</set>" +
            " where pcba_device_type_id = #{pcbaDeviceType.pcbaDeviceTypeId}" +
            "</script>")
    Integer updatePcbaDeviceType(@Param("pcbaDeviceType") PcbaDeviceType pcbaDeviceType);

    @ResultMap(value = "pcbaDeviceTypeResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_TYPE + " where pcba_device_type_name_cn = #{pcbaDeviceTypeNameCn}")
    PcbaDeviceType getByName(@Param("pcbaDeviceTypeNameCn") String pcbaDeviceTypeNameCn);
}
